package com.kksoft.readcm.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.objects.m;
import com.kksoft.readcm.objects.l;
import com.kksoft.readcm.pro.R;

public class SettingsActivity extends PreferenceActivity
{
  CheckBoxPreference a = null;
  CheckBoxPreference b = null;
  CheckBoxPreference c = null;
  private Preference d = null;
  private Preference e = null;
  private PreferenceCategory preferenceCategory = null;
  private PreferenceCategory g = null;
  private Preference h = null;
  //private Context i;

  static void a(SettingsActivity settingsactivity, boolean flag)
  {
      settingsactivity.a(flag);
  }

  private void a(boolean flag)
  {
      if(flag)
      {
          PreferenceCategory preferencecategory = g;
          CheckBoxPreference checkboxpreference = c;
          preferencecategory.removePreference(checkboxpreference);
          PreferenceCategory preferencecategory1 = g;
          CheckBoxPreference checkboxpreference1 = b;
          preferencecategory1.removePreference(checkboxpreference1);
      } else
      {
          PreferenceCategory preferencecategory2 = g;
          CheckBoxPreference checkboxpreference2 = c;
          preferencecategory2.addPreference(checkboxpreference2);
          PreferenceCategory preferencecategory3 = g;
          CheckBoxPreference checkboxpreference3 = b;
          preferencecategory3.addPreference(checkboxpreference3);
      }
  }

  static void a(boolean flag, boolean flag1, boolean flag2)
  {
      Appconfig.getThis().d().b();
      l.a(flag, flag1, flag2);
      Appconfig.getThis().getItemAppObservable().o();
  }

  protected void onCreate(Bundle bundle)
  {
	  super.onCreate(bundle);
	  Window window = getWindow();
      Display display = getWindowManager().getDefaultDisplay();
      int j = display.getWidth();
      int k = display.getHeight();
      float f1 = j / 2;
      float f2 = k / 2;
      float f3;
      float f4;
      RectShape rectshape;
      ShapeDrawable shapedrawable;
      Paint paint;
      int i1;
      int j1;
      android.graphics.Shader.TileMode tilemode;
      RadialGradient radialgradient;
      if(f1 > f2)
          f3 = f1;
      else
          f3 = f2;
      f4 = (float)((double)f3 * 0.86D);
      rectshape = new RectShape();
      shapedrawable = new ShapeDrawable(rectshape);
      shapedrawable.setDither(true);
      paint = shapedrawable.getPaint();
      i1 = Color.parseColor("#424141");
      j1 = Color.parseColor("#313131");
      tilemode = android.graphics.Shader.TileMode.CLAMP;
      radialgradient = new RadialGradient(f1, f2, f4, i1, j1, tilemode);
      //android.graphics.Shader shader = 
    	  paint.setShader(radialgradient);
      window.setBackgroundDrawable(shapedrawable);
      //i = this;
      addPreferencesFromResource(R.xml.settings);
      preferenceCategory = (PreferenceCategory)findPreference("accountCategory");
      e = findPreference("version");
      StringBuilder stringbuilder = (new StringBuilder(String.valueOf(App.e().e()))).append(" ");
      stringbuilder.append(m.f());
      e.setSummary(stringbuilder.toString());
      g = (PreferenceCategory)findPreference("downloadingCategory");
      
      
      d = findPreference("logout");
      d.setOnPreferenceClickListener(new OnPreferenceClickListener(){

  		@Override
  		public boolean onPreferenceClick(Preference preference) {
  			// TODO Auto-generated method stub
  			SettingsActivity.this.showDialog(1);
  		    return true;
  		}
      	  
        });
      
      h = findPreference("adv");
      h.setOnPreferenceClickListener(new OnPreferenceClickListener(){

		@Override
		public boolean onPreferenceClick(Preference preference) {
			// TODO Auto-generated method stub
		    Intent it = new Intent(SettingsActivity.this, SettingsAdvActivity.class);
		    startActivity(it);
		    return true;
		}
    	  
      });
      
      c = (CheckBoxPreference)findPreference("downloadText");
      c.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
  		@Override
  		public boolean onPreferenceChange(Preference preference, Object newValue) {
  			// TODO Auto-generated method stub
  			boolean bool1 = SettingsActivity.this.a.isChecked();
  	    boolean bool2 = SettingsActivity.this.b.isChecked();
  	    boolean bool3 = ((Boolean)newValue).booleanValue();
  	    SettingsActivity.a(bool1, bool2, bool3);
  	    return true;
  		}
        });
      
      b = (CheckBoxPreference)findPreference("downloadWeb");
      b.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			// TODO Auto-generated method stub
			boolean bool1 = SettingsActivity.this.a.isChecked();
		    boolean bool2 = ((Boolean)newValue).booleanValue();
		    boolean bool3 = SettingsActivity.this.b.isChecked();
		    SettingsActivity.a(bool1, bool2, bool3);
		    return true;
		}
      });
      
      CheckBoxPreference a = (CheckBoxPreference)findPreference("downloadAutoPick");
      a.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			// TODO Auto-generated method stub
		    SettingsActivity.a(SettingsActivity.this, ((Boolean)newValue).booleanValue());
		    boolean bool2 = ((Boolean)newValue).booleanValue();
		    boolean bool3 = SettingsActivity.this.b.isChecked();
		    boolean bool4 = SettingsActivity.this.c.isChecked();
		    SettingsActivity.a(bool2, bool3, bool4);
		    return true;
		}
      });
      
      if(App.getSharedPreferences().getBoolean("downloadAutoPick", true))
          a(true);
      getListView().setCacheColorHint(0);
      
      Preference pCredits = findPreference("credits");
      pCredits.setOnPreferenceClickListener(new OnPreferenceClickListener(){
		@Override
		public boolean onPreferenceClick(Preference preference) {
			// TODO Auto-generated method stub
		    HelpActivity.a(10, SettingsActivity.this);
		    return true;
		}
    	  
      });
  }

  protected Dialog onCreateDialog(int paramInt)
  {  
    Dialog dialog;
    switch (paramInt)
    {
    default:
    	dialog = null;
      break;
    case 1:
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(getString(R.string.dg_confirm_logout_t))
    			.setMessage(getString(R.string.dg_confirm_logout_m))
    			.setPositiveButton(R.string.ac_yes,new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					arg0.dismiss();
					SettingsActivity.this.showDialog(2);
				    App.q().initSetting(SettingsActivity.this);
				}
             })
             .setNegativeButton(R.string.ac_cancel,new DialogInterface.OnClickListener(){
                 public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
                 }
             });
    	
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
    	break;
    case 2:
    	dialog = new ProgressDialog(this);
        ((ProgressDialog) (dialog)).setMessage(getString(R.string.dg_logging_out));
        ((ProgressDialog) (dialog)).setIndeterminate(true);
        ((ProgressDialog) (dialog)).setCancelable(false);
    	break;
    }
    return dialog;
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
      menu.add(-1, 6, 6, getString(R.string.mu_help)).setIcon(R.drawable.ic_menu_help);
      return super.onCreateOptionsMenu(menu);
  }

  public boolean onOptionsItemSelected(MenuItem menuitem)
  {
	  boolean flag;
    switch (menuitem.getItemId())
    {
    default:
    	flag = super.onOptionsItemSelected(menuitem);
    	break;
    case 6:
    	Context context = App.getAppThis();
        Intent intent = new Intent(context, HelpActivity.class);
        startActivity(intent);
        flag = true;
    	break;
    }
    return flag;
  }

  public void onPause()
  {
    App.setWarkful(3, null);
    super.onPause();
  }

  protected void onResume()
  {
    Appconfig.getThis().o();
    App.setWarkful(1, null);
    super.onResume();
    String str3 = String.format(getString(R.string.st_your_account), Appconfig.getThis().getLoginName());
    preferenceCategory.setTitle(str3);
  }

  public boolean onSearchRequested()
  {
    return false;
  }
}